package com.caioweb.osa.transacoes.interceptor;

import java.io.Serializable;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
@Interceptor @Transactional
public class TransactionInterception implements Serializable {
	@Inject 
	private EntityManager em;
	
	@AroundInvoke
	public Object intercept(InvocationContext ctx) throws Exception{
		System.out.println("Abrindo transacao");
		em.getTransaction().begin();
		Object resultado = ctx.proceed();
		
		em.getTransaction().commit();
		
		return resultado;
	}

}
